package com.jms.web.controller.jms.service.impl;


import javax.annotation.Resource;
import java.util.*;

import com.jms.common.core.domain.AjaxResult;
import com.jms.common.core.domain.entity.SysDept;
import com.jms.common.filter.utils.DateUtils;
import com.jms.common.filter.utils.SecurityUtils;
import com.jms.system.service.ISysDeptService;
import com.jms.web.controller.jms.domain.AttachFile;
import com.jms.web.controller.jms.domain.Enterprise;
import com.jms.web.controller.jms.domain.Question;
import com.jms.web.controller.jms.domain.QuestionSchedule;
import com.jms.web.controller.jms.mapper.AttachFileMapper;
import com.jms.web.controller.jms.mapper.EnterpriseMapper;
import com.jms.web.controller.jms.mapper.QuestionMapper;
import com.jms.web.controller.jms.mapper.QuestionScheduleMapper;
import com.jms.web.controller.jms.service.IQuestionService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 问题反映Service业务层处理
 *
 * @author drx
 * @date 2024-04-29
 */
@Service
public class QuestionServiceImpl implements IQuestionService {
    @Resource
    private QuestionMapper questionMapper;

    @Resource
    private AttachFileMapper attachFileMapper;

    @Resource
    private QuestionScheduleMapper questionScheduleMapper;

    @Resource
    private EnterpriseMapper enterpriseMapper;

    @Resource
    private ISysDeptService sysDeptService;

    /**
     * 查询问题反映
     *
     * @param id 问题反映主键
     * @return 问题反映
     */
    @Override
    public Question selectQuestionById(Long id) {
        Question question = questionMapper.selectQuestionById(id);
        if (question != null) {
            /*查询当前问题的流程信息*/
            QuestionSchedule questionSchedule = new QuestionSchedule();
            questionSchedule.setQuestionId(question.getId());
            List<QuestionSchedule> questionSchedules = questionScheduleMapper.selectQuestionScheduleList(questionSchedule);
            question.setQuestionSchedules(questionSchedules);
            /*查询当前问题的企业详情*/
            if (question.getEnterpriseId() != null) {
                Enterprise enterprise = enterpriseMapper.selectEnterpriseById(question.getEnterpriseId());
                question.setEnterprise(enterprise);
            }
            /*查询当前问题相关文件*/
            AttachFile attachFile = new AttachFile();
            attachFile.setSourceId(question.getId());
            attachFile.setFileClass("01");
            List<AttachFile> attachFiles = attachFileMapper.selectAttachFileList(attachFile);
            question.setAttachFileList(attachFiles);
        }
        return question;
    }

    /**
     * 查询问题反映列表
     *
     * @param question 问题反映
     * @return 问题反映
     */
    @Override
    public List<Question> selectQuestionList(Question question) {
        return questionMapper.selectQuestionList(question);
    }

    /**
     * 新增问题反映
     *
     * @param question 问题反映
     * @return 结果
     */
    @Override
    public int insertQuestion(Question question) {
        question.setCreateTime(DateUtils.getNowDate());
        question.setCreateName(SecurityUtils.getLoginUser().getUser().getNickName());
        question.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        int i = questionMapper.insertQuestion(question);
        //插入问题图片
        List<AttachFile> list = question.getAttachFileList();
        if (list != null) {
            list.forEach(item -> {
                item.setFileClass("01");
                item.setSourceId(question.getId());
                item.setCreateTime(DateUtils.getNowDate());
                item.setCreateName(SecurityUtils.getLoginUser().getUser().getNickName());
                item.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
                attachFileMapper.insertAttachFile(item);
            });
        }
        return i;
    }

    /**
     * 修改问题反映
     *
     * @param question 问题反映
     * @return 结果
     */
    @Override
    public int updateQuestion(Question question) {
        question.setUpdateTime(DateUtils.getNowDate());
        question.setUpdateName(SecurityUtils.getLoginUser().getUser().getNickName());
        question.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        /*删除原有的相关文件 并报存新上传的文件*/
        attachFileMapper.deleteAttachFileBySourceId(question.getId());
        List<AttachFile> list = question.getAttachFileList();
        if (list != null) {
            list.forEach(item -> {
                item.setFileClass("01");
                item.setSourceId(question.getId());
                item.setCreateTime(DateUtils.getNowDate());
                item.setCreateName(SecurityUtils.getLoginUser().getUser().getNickName());
                item.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
                attachFileMapper.insertAttachFile(item);
            });
        }

        if (question.getResolved() == 1) {
            question.setResolvedName(SecurityUtils.getLoginUser().getUser().getNickName());
            question.setResolvedTime(DateUtils.getNowDate());
            QuestionSchedule questionSchedule = new QuestionSchedule();
            questionSchedule.setCreateName(SecurityUtils.getUsername());
            questionSchedule.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            questionSchedule.setCreateTime(DateUtils.getNowDate());
            questionSchedule.setFlowContent("已解决");
            questionSchedule.setQuestionId(question.getId());
            questionScheduleMapper.insertQuestionSchedule(questionSchedule);
        } else if (question.getResolved() != 1) {
            questionScheduleMapper.deleteQuestionScheduleByQuestionid(question.getId());
            if (question.getQuestionSchedules() != null) {
                List<QuestionSchedule> questionSchedules = question.getQuestionSchedules();
                Collections.reverse(questionSchedules);
                questionSchedules.forEach(bean -> {
                    bean.setQuestionId(question.getId());
                    bean.setCreateName(bean.getCreateName() == null ? SecurityUtils.getLoginUser().getUser().getNickName() : bean.getCreateName());
                    bean.setCreateBy(bean.getCreateBy() == null ? String.valueOf(SecurityUtils.getUserId()) : bean.getCreateBy());
                    bean.setCreateTime(bean.getCreateTime() == null ? DateUtils.getNowDate() : bean.getCreateTime());
                    questionScheduleMapper.insertQuestionSchedule(bean);
                });
            }
        }

        return questionMapper.updateQuestion(question);
    }

    /**
     * 修改问题反映
     *
     * @param question 问题反映
     * @return 结果
     */
    @Override
    public int editQuestion(Question question) {
        question.setUpdateTime(DateUtils.getNowDate());
        question.setUpdateName(SecurityUtils.getLoginUser().getUser().getNickName());
        question.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        return questionMapper.updateQuestion(question);
    }

    /**
     * 批量删除问题反映
     *
     * @param ids 需要删除的问题反映主键
     * @return 结果
     */
    @Override
    public int deleteQuestionByIds(Long[] ids) {
        return questionMapper.deleteQuestionByIds(ids);
    }

    /**
     * 删除问题反映信息
     *
     * @param id 问题反映主键
     * @return 结果
     */
    @Override
    public int deleteQuestionById(Long id) {
        return questionMapper.deleteQuestionById(id);
    }

    @Override
    public AjaxResult getStatistics(){
        AjaxResult ajaxResult = new AjaxResult();
        try{

            /*
             * 获取当前用户部门区划编码
             * */
            Question question = new Question();

            Long deptId = SecurityUtils.getLoginUser().getDeptId();
            SysDept sysDept = sysDeptService.selectDeptById(deptId);
            String areaShortCode = sysDept.getAreaShortCode();

            areaShortCode = areaShortCode.replaceAll("0+$", "").replaceAll("\\.$", "");

            question.setAreaShortCode(areaShortCode);

            /*获取问题数量占比*/
            List<HashMap<String, Object>> quesTypeList = questionMapper.selectQuesType(question);

            ajaxResult.put("data",quesTypeList);
            ajaxResult.put("code",200);
            ajaxResult.put("msg","获取统计信息成功");

        }catch (Exception e){
            e.printStackTrace();
            ajaxResult.put("data",null);
            ajaxResult.put("code",500);
            ajaxResult.put("msg","获取统计信息失败,请联系管理员");
        }
        return ajaxResult;
    }
}
